<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 组织架构模块 -->
      <el-card>
        <!-- 头部部分 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="company" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="toAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体部分 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
            #default === slot-scope
            什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
          -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="branch" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="toAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="toEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="toDelete(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="isEdit ? '修改' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hDialogClose"
      >
        <!-- 子组件 -->
        <add-or-eidt
          :id="curId"
          ref="deptDialogRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, deleteDepartments } from '@/api/departments' // 查询企业的部门列表
import { tranListToTreeData } from '@/utils' // 工具函数将平铺数组转化为树行数组
import addOrEidt from './deptDialog.vue'
export default {
  components: {
    addOrEidt // 注册子组件
  },
  data() {
    return {
      list: [], // 存储tree中数据
      showDialog: false, // 控制弹框显示隐藏
      curId: '',
      isEdit: false, // 控制显示添加还是编辑 弹框
      originList: [] // 初始部门数据
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('查询企业的部门列表', res)
        // 筛选需要的数据项
        this.originList = res.data.depts.map((item) => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        // shift会取出数组第一个元素，同时会改变原数组 -> 删除第一个元素 返回剩下的元素
        const company = res.data.depts.shift()
        console.log('删除的第一个部门', company)
        // this.list = res.data.depts
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    toAdd(pid) {
      this.showDialog = true
      this.curId = pid
    },
    // 子传父->自定义事件
    hSuccess() {
      // 关闭弹窗 重新渲染部门列表
      this.loadDepartments()
      this.showDialog = false
    },
    // 编辑部门
    toEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 引入子组件
      this.$nextTick(() => {
        this.$refs.deptDialogRef.loadDepartmentsDetail()
      })
    },
    // 删除部门
    toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 确认删除
          const res = await deleteDepartments(id)
          console.log('删除部门', res)
          this.loadDepartments()
        })
        .catch(() => {
          // 取消删除
        })
    },
    // 引用子组件 调用子组件方法
    hDialogClose() {
      this.$refs.deptDialogRef.resetForm()
    }
  }
}
</script>
